package cn.hahah.module.blog.controller.admin.sysnotice;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.sysnotice.vo.*;
import cn.hahah.module.blog.dal.dataobject.sysnotice.SysNoticeDO;
import cn.hahah.module.blog.service.sysnotice.SysNoticeService;

@Tag(name = "管理后台 - 博客系统公告")
@RestController
@RequestMapping("/blog/sys-notice")
@Validated
public class SysNoticeController {

    @Resource
    private SysNoticeService sysNoticeService;

    @PostMapping("/create")
    @Operation(summary = "创建博客系统公告")
    @PreAuthorize("@ss.hasPermission('blog:sys-notice:create')")
    public CommonResult<Long> createSysNotice(@Valid @RequestBody SysNoticeSaveReqVO createReqVO) {
        return success(sysNoticeService.createSysNotice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新博客系统公告")
    @PreAuthorize("@ss.hasPermission('blog:sys-notice:update')")
    public CommonResult<Boolean> updateSysNotice(@Valid @RequestBody SysNoticeSaveReqVO updateReqVO) {
        sysNoticeService.updateSysNotice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除博客系统公告")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:sys-notice:delete')")
    public CommonResult<Boolean> deleteSysNotice(@RequestParam("id") Long id) {
        sysNoticeService.deleteSysNotice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得博客系统公告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:sys-notice:query')")
    public CommonResult<SysNoticeRespVO> getSysNotice(@RequestParam("id") Long id) {
        SysNoticeDO sysNotice = sysNoticeService.getSysNotice(id);
        return success(BeanUtils.toBean(sysNotice, SysNoticeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得博客系统公告分页")
    @PreAuthorize("@ss.hasPermission('blog:sys-notice:query')")
    public CommonResult<PageResult<SysNoticeRespVO>> getSysNoticePage(@Valid SysNoticePageReqVO pageReqVO) {
        PageResult<SysNoticeDO> pageResult = sysNoticeService.getSysNoticePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SysNoticeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出博客系统公告 Excel")
    @PreAuthorize("@ss.hasPermission('blog:sys-notice:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSysNoticeExcel(@Valid SysNoticePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SysNoticeDO> list = sysNoticeService.getSysNoticePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "博客系统公告.xls", "数据", SysNoticeRespVO.class,
                        BeanUtils.toBean(list, SysNoticeRespVO.class));
    }

}